package com.ytjj.qmyx.admin.utils;

import com.ytjj.qmyx.admin.model.Menu;
import com.ytjj.qmyx.admin.model.response.ButtonViewsResponse;
import com.ytjj.qmyx.admin.model.response.MenuTreeResponse;
import com.ytjj.qmyx.admin.model.response.MenuViewsResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TreeUtil {

    public static List<MenuTreeResponse> getTreeList(List<MenuTreeResponse> entityList) {
        List<MenuTreeResponse> resultList = new ArrayList<>();
      //获取顶层元素集合
        String parentCode;
        for (MenuTreeResponse entity : entityList) {
            parentCode = entity.getMenuPreCode();               // 父级菜单码
            //顶层元素的parentCode==null
            if (StringUtils.isBlank(parentCode)) {
                resultList.add(entity);
            }
        }
        //获取每个顶层元素的子数据集合
        for (MenuTreeResponse entity : resultList) {
               entity.setChildren(getSubList(String.valueOf(entity.getId()), entityList));
        }
        return resultList;
    }

    private static List<MenuTreeResponse> getSubList(String menuCode, List<MenuTreeResponse> entityList) {
        List<MenuTreeResponse> childList = new ArrayList<>();
        //子集的直接子对象
        for (MenuTreeResponse entity : entityList) {
            String parentCode = entity.getMenuPreCode();
            if (menuCode.equals(parentCode)) {
                childList.add(entity);
            }
        }
        //子集的间接子对象
        for (MenuTreeResponse entity : childList) {
            entity.setChildren(getSubList(String.valueOf(entity.getId()), entityList));
        }
        //递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }

    // 进一步处理数据
    public static List<MenuTreeResponse> getCloneData(List<Menu> menus){
        if(menus == null || menus.size()<=0){
            return null;
        }
        List<MenuTreeResponse> menuTreeResponses = new ArrayList<>();
        for(Menu menu : menus){
            MenuTreeResponse menuTreeResponse = new MenuTreeResponse();
            menuTreeResponse.setId(menu.getId());
            menuTreeResponse.setMenuCode(menu.getMenuCode());
            menuTreeResponse.setMenuName(menu.getMenuName());
            menuTreeResponse.setMenuLevel(menu.getMenuLevel());
            menuTreeResponse.setIcon(menu.getIcon());
            menuTreeResponse.setHidden(menu.getHidden());
            menuTreeResponse.setIsPermission(menu.getIsPermission());
            menuTreeResponse.setMenuPreCode(menu.getMenuPreCode());
            menuTreeResponse.setType(menu.getType());
            menuTreeResponse.setUrl(menu.getUrl());
            menuTreeResponse.setSort(menu.getSort());
            menuTreeResponse.setTitle(menu.getTitle());
            menuTreeResponse.setRouteUrl(menu.getRouteUrl());
            menuTreeResponse.setCreateTime(menu.getCreateTime());
            menuTreeResponse.setUpdateTime(menu.getUpdateTime());
            menuTreeResponses.add(menuTreeResponse);
        }
        return menuTreeResponses;
    }

    public static List<MenuViewsResponse> getRouteTreeList(List<MenuViewsResponse> entityList) {
        List<MenuViewsResponse> resultList = new ArrayList<>();
        //获取顶层元素集合
        String parentCode;
        for (MenuViewsResponse entity : entityList) {
            parentCode = entity.getMenuPreCode();               // 父级菜单码
            //顶层元素的parentCode==null
            if (StringUtils.isBlank(parentCode)) {
                resultList.add(entity);
            }
        }
        //获取每个顶层元素的子数据集合
        for (MenuViewsResponse entity : resultList) {
            entity.setChildren(getRouteSubList(entity.getId(), entityList));
        }
        return resultList;
    }

    private static List<MenuViewsResponse> getRouteSubList(String menuCode, List<MenuViewsResponse> entityList) {
        List<MenuViewsResponse> childList = new ArrayList<>();
        //子集的直接子对象
        for (MenuViewsResponse entity : entityList) {
            String parentCode = entity.getMenuPreCode();
            if (menuCode.equals(parentCode)) {
                childList.add(entity);
            }
        }
        //子集的间接子对象
        for (MenuViewsResponse entity : childList) {
            entity.setChildren(getRouteSubList(entity.getId(), entityList));
        }
        //递归退出条件
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }

    // 进一步处理数据
    public static List<MenuViewsResponse> getRouteListData(List<Menu> menus){
        if(menus == null || menus.size()<=0){
            return null;
        }
        List<MenuViewsResponse> menuViewsResponses = new ArrayList<>();
        for(Menu menu : menus){
            MenuViewsResponse menuViewsResponse = new MenuViewsResponse();
            Map meta = new HashMap();
            menuViewsResponse.setId(String.valueOf(menu.getId()));
            menuViewsResponse.setMenuPreCode(menu.getMenuPreCode());
            menuViewsResponse.setPath(menu.getUrl());
            menuViewsResponse.setName(menu.getMenuName());
            menuViewsResponse.setHidden(menu.getHidden());
            meta.put("title",menu.getTitle());
            meta.put("icon",menu.getIcon());
            menuViewsResponse.setMeta(meta);
            menuViewsResponse.setSort(menu.getSort());
            menuViewsResponse.setComponent(menu.getMenuCode());
            menuViewsResponse.setRedirect(menu.getRouteUrl());
            menuViewsResponses.add(menuViewsResponse);
        }
        return menuViewsResponses;
    }

}
